//
//  OAuthViewController+Oauth.swift
//  MyLottery
//
//  Created by wujunjie on 2018/7/31.
//  Copyright © 2018年 wujunjie. All rights reserved.
//
let rediretURL = "https://baidu.com"
let appKey = "4255870175"
let appSecret = "144b29fa0f76d0522fd70a7ec01b1e29"
let weiBoId = "18630310482"
let weiBoPassword = "wjj080415"
import Alamofire
//MARK:--------获取授权的令牌
public func getAccessToken(code:String,finish:@escaping (AnyObject?)->()){
    //根据 code获取 accessToken
    let accessTokenUrlStr = "https://api.weibo.com/oauth2/access_token"
    let parameters = ["client_id":appKey,"client_secret":appSecret,"grant_type": "authorization_code","code":code,"redirect_uri":rediretURL]
    Alamofire.request(accessTokenUrlStr, method: .post, parameters: parameters).responseJSON { (response) in
        guard let result =  response.result.value else {
            print(response.result.error!)
            return
        }
        print(result)
        finish(result as AnyObject)
    }
}

//MARK:-------根据accssToken 和 uid 获取用户信息
public func getUserInfo(accessToken:String,uid:UInt64,finish:@escaping(AnyObject?)->()){
    let userRequestUrl = "https://api.weibo.com/2/users/show.json?access_token=\(accessToken)&uid=\(uid)"
    Alamofire.request(userRequestUrl, method: .get).responseJSON { (response) in
        guard let result =  response.result.value else {
            print(response.result.error!)
            return
        }
        finish(result as AnyObject)
    }
}
